package app.mvp.base;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import app.mvp.base.mvp.BasePresenter;
import app.mvp.base.mvp.BaseView;
import app.mvp.utils.OnClickUtil;

/**
 * Created by 刘智强 on 2017/4/6.
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView, View.OnClickListener {

    private P mPresenter;
    private Context mContext;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext=activity;
        mPresenter.attachView(this,mContext);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(getLayoutResource(), container, false);
        initPresenter();
        initView();
        initListener();
        return view;
    }

    public abstract int getLayoutResource();

    public abstract void initPresenter();

    public abstract void initView();

    public abstract void initClick(View v);

    public abstract void initListener();

    @Override
    public void onClick(View v) {
        if (OnClickUtil.fastClick()) {
            initClick(v);
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.detachView();
    }
}
